@echo off
chcp 65001 >nul
title zhub 构建脚本

rem 获取当前日期和时间并格式化为 YYYY.MM.DD-HH.MM.SS
for /f "tokens=2 delims==" %%i in ('wmic os get localdatetime /value') do set datetime=%%i
set year=%datetime:~0,4%
set month=%datetime:~4,2%
set day=%datetime:~6,2%
set hour=%datetime:~8,2%
set minute=%datetime:~10,2%
set second=%datetime:~12,2%
set version=%year%.%month%.%day%-%hour%.%minute%.%second%

rem 输出当前版本号
echo 当前构建版本号：%version%

rem 删除旧文件
echo 删除历史构建文件...
del /q zhub zhub.sh zhub.exe >nul 2>nul

rem 交互式平台选择
echo ==============================
echo     请选择要构建的平台：
echo ==============================
echo 1. Linux (amd64)
echo 2. Linux (arm64)
echo 3. Windows (amd64)
echo 4. MacOS (amd64)
echo 5. 全部构建
echo 0. 退出
echo ==============================
set /p choice=请输入选项编号（可多选，例如 135）:

if "%choice%"=="0" goto end

if not "%choice%"=="" (
    echo 开始构建版本 %version%...
)

if "%choice%"=="1" (
    call :build_linux
)
if "%choice%"=="2" (
    call :build_linux_arm
)
if "%choice%"=="3" (
    call :build_windows
)
if "%choice%"=="4" (
    call :build_mac
)
if "%choice%"=="5" (
    call :build_linux
    call :build_linux_arm
    call :build_windows
    call :build_mac
)

goto end

rem ========= 构建平台函数 ==========

:build_linux
echo [Linux amd64] 开始构建...
set GOOS=linux
set GOARCH=amd64
go build -o zhub.sh -ldflags "-s -w -X 'zhub/internal/monitor.Version=%version%'"
upx -9 zhub.sh
call :move_to_dist zhub.sh linux
goto :eof

:build_linux_arm
echo [Linux arm64] 开始构建...
set GOOS=linux
set GOARCH=arm64
go build -o zhub.sh -ldflags "-s -w -X 'zhub/internal/monitor.Version=%version%'"
upx -9 zhub.sh
call :move_to_dist zhub.sh linux-arm
goto :eof

:build_windows
echo [Windows amd64] 开始构建...
set GOOS=windows
set GOARCH=amd64
go build -o zhub.exe -ldflags "-s -w -X 'zhub/internal/monitor.Version=%version%'"
upx -9 zhub.exe
call :move_to_dist zhub.exe windows
goto :eof

:build_mac
echo [MacOS amd64] 开始构建...
set GOOS=darwin
set GOARCH=amd64
go build -o zhub -ldflags "-s -w -X 'zhub/internal/monitor.Version=%version%'"
upx -9 zhub
call :move_to_dist zhub mac
goto :eof

rem ========= 移动构建文件 ==========

:move_to_dist
setlocal
set file=%1
set platform=%2
set target=dist\%version%\%platform%
if not exist %target% (
    mkdir %target%
)
move /Y %file% %target%\ >nul
echo 构建产物已移动至：%target%\%file%
endlocal
goto :eof

:end
echo 构建流程结束。
pause
exit
